<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"wan-lei8.gitee.io","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="当有人问你如何对数据库进行优化时，很多人第一反应想到的就是SQL优化，如何创建索引，如何改写SQL，他们把数据库优化与SQL优化划上了等号。 当然这不能算是完全错误的回答，只不过思考的角度稍微片面了些，太“程序员思维”化了，没有站在更高层次来思考回答。那今天我们就将视角拔高，站在架构的角度来聊聊这一问题，数据库优化可以从哪些维度入手？  正如上图所示，数据库优化可以从架构优化，硬件优化，DB优化，">
<meta property="og:type" content="article">
<meta property="og:title" content="数据库优化，可以从哪些维度入手">
<meta property="og:url" content="https://wan-lei8.gitee.io/2022/01/24/%E6%95%B0%E6%8D%AE%E5%BA%93%E4%BC%98%E5%8C%96%EF%BC%8C%E5%8F%AF%E4%BB%A5%E4%BB%8E%E5%93%AA%E4%BA%9B%E7%BB%B4%E5%BA%A6%E5%85%A5%E6%89%8B/index.html">
<meta property="og:site_name" content="程序员西桐">
<meta property="og:description" content="当有人问你如何对数据库进行优化时，很多人第一反应想到的就是SQL优化，如何创建索引，如何改写SQL，他们把数据库优化与SQL优化划上了等号。 当然这不能算是完全错误的回答，只不过思考的角度稍微片面了些，太“程序员思维”化了，没有站在更高层次来思考回答。那今天我们就将视角拔高，站在架构的角度来聊聊这一问题，数据库优化可以从哪些维度入手？  正如上图所示，数据库优化可以从架构优化，硬件优化，DB优化，">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://mmbiz.qpic.cn/mmbiz_png/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZUBOOAaRUPb6icJ8Z7QpcPppzsELhnhlQ2xaEWBQADUEPzob4u24S29A/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1">
<meta property="og:image" content="https://mmbiz.qpic.cn/mmbiz_png/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZsqbXm4MvOrY4Zp9nzzRjQ4snXthzdKu3V53vibG8YKjtdM1RFOohPZg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1">
<meta property="og:image" content="https://mmbiz.qpic.cn/mmbiz_png/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZURWyaWhvNTLrMrfjY8Jia74eibjib9618AN9zLEyZCwKywrahLibGoNCAw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1">
<meta property="og:image" content="https://mmbiz.qpic.cn/mmbiz_png/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZOak7Sia3jBewQjvhA7ynrIhXVSgQu91LYuCo1GTQvicHcFgE7JNickxtQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1">
<meta property="og:image" content="https://mmbiz.qpic.cn/mmbiz_png/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZmqM1EWticOELy4ia0mpXbDnJJmkiaKtDvHVgwuXCZ3oyicZdo0cVd7onxg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1">
<meta property="og:image" content="https://mmbiz.qpic.cn/mmbiz_png/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZoaX5lLUV2Gibp1ATI99ia0Uyc92Gh29slwOR9NqlXd3AjFQfibC3B5kWA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1">
<meta property="og:image" content="https://mmbiz.qpic.cn/mmbiz_jpg/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZMkGXYv94jHf69fRH5LDnBw5cCuDXOrKjEtrCSJK5OLKVsV76VGRwRw/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1">
<meta property="og:image" content="https://mmbiz.qpic.cn/mmbiz_jpg/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZs3xtFkKwUog80SviaOez5vBWrQwvXgFMibjZpAKiaRCFS7sxjV4jObVhA/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1">
<meta property="og:image" content="https://mmbiz.qpic.cn/mmbiz_jpg/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZB3WFry1l3C8ZrM1dBRibGekQyuhicPF73PuZufAdFK8icZxvxZcYWSGZw/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1">
<meta property="og:image" content="https://mmbiz.qpic.cn/mmbiz_jpg/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZ5WS5TVeo9hicYspiaFhfh8VuXm8KhFqEBwUThibHgG4CMTM8k3x1lIiaJQ/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1">
<meta property="og:image" content="https://mmbiz.qpic.cn/mmbiz_jpg/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZbicErgVnxzg5zib5EDfAzIOrresMicBupJqfMrkNAHbQXLH5Nha3dp2yw/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1">
<meta property="og:image" content="https://mmbiz.qpic.cn/mmbiz_jpg/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZbviaibib2lRC2wLsc7V1bhx6jBoKoUL2P2T0utdMiaLPAibxdjpjua2GHqQ/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1">
<meta property="og:image" content="https://mmbiz.qpic.cn/mmbiz_jpg/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZq4MvsGjiahxaQWgUep0QZ01iccsTCqZeQAxS9xpwN8dSeX0yKSEAuQkg/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1">
<meta property="article:published_time" content="2022-01-24T09:02:43.000Z">
<meta property="article:modified_time" content="2022-01-24T09:03:26.887Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://mmbiz.qpic.cn/mmbiz_png/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZUBOOAaRUPb6icJ8Z7QpcPppzsELhnhlQ2xaEWBQADUEPzob4u24S29A/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1">

<link rel="canonical" href="https://wan-lei8.gitee.io/2022/01/24/%E6%95%B0%E6%8D%AE%E5%BA%93%E4%BC%98%E5%8C%96%EF%BC%8C%E5%8F%AF%E4%BB%A5%E4%BB%8E%E5%93%AA%E4%BA%9B%E7%BB%B4%E5%BA%A6%E5%85%A5%E6%89%8B/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>数据库优化，可以从哪些维度入手 | 程序员西桐</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">程序员西桐</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">但行好事，莫问前程</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://wan-lei8.gitee.io/2022/01/24/%E6%95%B0%E6%8D%AE%E5%BA%93%E4%BC%98%E5%8C%96%EF%BC%8C%E5%8F%AF%E4%BB%A5%E4%BB%8E%E5%93%AA%E4%BA%9B%E7%BB%B4%E5%BA%A6%E5%85%A5%E6%89%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.png">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="程序员西桐">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          数据库优化，可以从哪些维度入手
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2022-01-24 17:02:43 / 修改时间：17:03:26" itemprop="dateCreated datePublished" datetime="2022-01-24T17:02:43+08:00">2022-01-24</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>当有人问你如何对数据库进行优化时，很多人第一反应想到的就是SQL优化，如何创建索引，如何改写SQL，他们把数据库优化与SQL优化划上了等号。</p>
<p>当然这不能算是完全错误的回答，只不过思考的角度稍微片面了些，太“<strong>程序员思维</strong>”化了，没有站在更高层次来思考回答。那今天我们就将视角拔高，站在架构的角度来聊聊这一问题，<strong>数据库优化可以从哪些维度入手？</strong></p>
<p><img src="https://mmbiz.qpic.cn/mmbiz_png/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZUBOOAaRUPb6icJ8Z7QpcPppzsELhnhlQ2xaEWBQADUEPzob4u24S29A/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" alt="图片"></p>
<p>正如上图所示，数据库优化可以从架构优化，硬件优化，DB优化，SQL优化四个维度入手。</p>
<p>此上而下，位置越靠前优化越明显，对数据库的性能提升越高。我们常说的SQL优化反而是对性能提高最小的优化。</p>
<p>接下来我们再看看每种优化该如何实施。</p>
<h2 id="架构优化"><a href="#架构优化" class="headerlink" title="架构优化"></a>架构优化</h2><p>一般来说在高并发的场景下对架构层进行优化其效果最为明显，常见的优化手段有：分布式缓存，读写分离，分库分表等，每种优化手段又适用于不同的应用场景。</p>
<h3 id="分布式缓存"><a href="#分布式缓存" class="headerlink" title="分布式缓存"></a>分布式缓存</h3><p>有句老话说的好，性能不够，缓存来凑。当需要在架构层进行优化时我们第一时间就会想到缓存这个神器，在应用与数据库之间增加一个缓存服务，如Redis或Memcache。</p>
<p><img src="https://mmbiz.qpic.cn/mmbiz_png/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZsqbXm4MvOrY4Zp9nzzRjQ4snXthzdKu3V53vibG8YKjtdM1RFOohPZg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" alt="图片"></p>
<p>当接收到查询请求后，我们先查询缓存，判断缓存中是否有数据，有数据就直接返回给应用，如若没有再查询数据库，并加载到缓存中，这样就大大减少了对数据库的访问次数，自然而然也提高了数据库性能。</p>
<p>不过需要注意的是，引入分布式缓存后系统需要考虑如何应对<strong>缓存穿透、缓存击穿和缓存雪崩</strong>的问题。</p>
<blockquote>
<p>“</p>
<p>简单理解一下 缓存穿透、缓存击穿 和 缓存雪崩</p>
<p>缓存穿透：它是指当用户在查询一条数据的时候，而此时数据库和缓存都没有关于这条数据的任何记录。这条数据在缓存中没找到就会向数据库请求获取数据。它拿不到数据时，是会一直查询数据库，这样会对数据库的访问造成很大的压力。</p>
<p>缓存击穿：一个热点key刚好在某个时间点失效了，但是这时候突然来了大量对这个key的并发访问请求，导致大并发请求直接穿透缓存直达数据库，瞬间对数据库的访问压力增大。</p>
<p>缓存雪崩：某一个时间段内，缓存集中过期失效，如果这个时间段内有大量请求，而查询数据量巨大，所有的请求都会达到存储层，存储层的调用量会暴增，引起数据库压力过大甚至宕机。</p>
<p>”</p>
</blockquote>
<h3 id="读写分离"><a href="#读写分离" class="headerlink" title="读写分离"></a>读写分离</h3><p>一主多从，读写分离，主动同步，是一种常见的数据库架构优化手段。</p>
<p>一般来说当你的应用是<strong>读多写少</strong>，数据库扛不住<strong>读压力</strong>的时候，采用读写分离，通过增加从库数量可以线性提升系统读性能。</p>
<p><img src="https://mmbiz.qpic.cn/mmbiz_png/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZURWyaWhvNTLrMrfjY8Jia74eibjib9618AN9zLEyZCwKywrahLibGoNCAw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" alt="图片"></p>
<p>主库，提供数据库写服务；从库，提供数据库读能力；主从之间，通过binlog同步数据。</p>
<p>当准备实施读写分离时，为了保证高可用，需要实现故障的自动转移，主从架构会有潜在主从不一致性问题。</p>
<h3 id="水平切分"><a href="#水平切分" class="headerlink" title="水平切分"></a>水平切分</h3><p>水平切分，也是一种常见的数据库架构优化手段。</p>
<p>当你的应用<strong>业务数据量很大</strong>，<strong>单库容量成为性能瓶颈后</strong>，采用水平切分，可以降低数据库单库容量，提升数据库写性能。</p>
<p><img src="https://mmbiz.qpic.cn/mmbiz_png/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZOak7Sia3jBewQjvhA7ynrIhXVSgQu91LYuCo1GTQvicHcFgE7JNickxtQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" alt="图片"></p>
<p>当准备实施水平切分时，需要结合实际业务选取合理的分片键（sharding-key），有时候为了解决非分片键查询问题还需要将数据写到单独的查询组件，如ElasticSearch。</p>
<h3 id="架构优化小结"><a href="#架构优化小结" class="headerlink" title="架构优化小结"></a>架构优化小结</h3><ol>
<li>读写分离主要是用于解决 “数据库读性能问题”</li>
<li>水平切分主要是用于解决“数据库数据量大的问题”</li>
<li>分布式缓存架构可能比读写分离更适用于高并发、大数据量大场景。</li>
</ol>
<h2 id="硬件优化"><a href="#硬件优化" class="headerlink" title="硬件优化"></a>硬件优化</h2><p>我们使用数据库，不管是读操作还是写操作，最终都是要访问磁盘，所以说磁盘的性能决定了数据库的性能。一块PCIE固态硬盘的性能是普通机械硬盘的几十倍不止。这里我们可以从<strong>吞吐率</strong>、<strong>IOPS</strong>两个维度看一下机械硬盘、普通固态硬盘、PCIE固态硬盘之间的性能指标。</p>
<p><strong>吞吐率：单位时间内读写的数据量</strong></p>
<ul>
<li>机械硬盘：约100MB/s ～ 200MB/s</li>
<li>普通固态硬盘：200MB/s ～ 500MB/s</li>
<li>PCIE固态硬盘：900MB/s ～ 3GB/s</li>
</ul>
<p><strong>IOPS：每秒IO操作的次数</strong></p>
<ul>
<li>机械硬盘：100 ～200</li>
<li>普通固态硬盘：30000 ～ 50000</li>
<li>PCIE固态硬盘：数十万</li>
</ul>
<p>通过上面的数据可以很直观的看到不同规格的硬盘之间的性能差距非常大，当然性能更好的硬盘价格会更贵，在资金充足并且迫切需要提升数据库性能时，尝试更换一下数据库的硬盘不失为一个非常好的举措，你之前遇到SQL执行缓慢问题在你更换硬盘后很可能将不再是问题。</p>
<h2 id="DB优化"><a href="#DB优化" class="headerlink" title="DB优化"></a>DB优化</h2><p>SQL执行慢有时候不一定完全是SQL问题，手动安装一台数据库而不做任何参数调整，再怎么优化SQL都无法让其性能最大化。要让一台数据库实例完全发挥其性能，首先我们就得先优化数据库的实例参数。</p>
<p>数据库实例参数优化遵循三句口诀：<strong>日志不能小、缓存足够大、连接要够用。</strong></p>
<p>数据库事务提交后需要将事务对数据页的修改刷（ fsync）到磁盘上，才能保证数据的持久性。这个刷盘，是一个随机写，性能较低，如果每次事务提交都要刷盘，会极大影响数据库的性能。数据库在架构设计中都会采用如下两个优化手法：</p>
<ul>
<li>先将事务写到日志文件RedoLog（WAL），将随机写优化成顺序写</li>
<li>加一层缓存结构Buffer，将单次写优化成顺序写</li>
</ul>
<p>所以日志跟缓存对数据库实例尤其重要。而连接如果不够用，数据库会直接抛出异常，系统无法访问。</p>
<p>接下来我们以Oracle、MySQL（InnoDB）、POSTGRES、达梦为例，看看每种数据库的参数该如何配置。</p>
<h3 id="Oracle"><a href="#Oracle" class="headerlink" title="Oracle"></a>Oracle</h3><table>
<thead>
<tr>
<th align="left">参数分类</th>
<th align="left">参数名</th>
<th align="left">参数值</th>
<th align="left">备注</th>
</tr>
</thead>
<tbody><tr>
<td align="left">数据缓存</td>
<td align="left">SGA_TAGET、MEMORY_TARGET</td>
<td align="left">物理内存70-80%</td>
<td align="left">越大越好</td>
</tr>
<tr>
<td align="left">数据缓存</td>
<td align="left">DB_CACHE_SIZE</td>
<td align="left">物理内存70-80%</td>
<td align="left">越大越好</td>
</tr>
<tr>
<td align="left">SQL解析</td>
<td align="left">SHARED_POOL_SIZE</td>
<td align="left">4-16G</td>
<td align="left">不建议设置过大</td>
</tr>
<tr>
<td align="left">监听及连接</td>
<td align="left">PROCESSES、SESSIONS、OPEN_CURSORS</td>
<td align="left">根据业务需求设置</td>
<td align="left">一般为业务预估连接数的120%</td>
</tr>
<tr>
<td align="left">其他</td>
<td align="left">SESSION_CACHED_CURSORS</td>
<td align="left">大于200</td>
<td align="left">软软解析</td>
</tr>
</tbody></table>
<h3 id="MySQL"><a href="#MySQL" class="headerlink" title="MySQL"></a>MySQL</h3><table>
<thead>
<tr>
<th align="left">参数分类</th>
<th align="left">参数名</th>
<th align="left">参数值</th>
<th align="left">备注</th>
</tr>
</thead>
<tbody><tr>
<td align="left">数据缓存</td>
<td align="left">INNODB_BUFFER_POOL_SIZE</td>
<td align="left">物理内存50-80%</td>
<td align="left">一般来说越大性能越好</td>
</tr>
<tr>
<td align="left">日志相关</td>
<td align="left">Innodb_log_buffer_size</td>
<td align="left">16-32M</td>
<td align="left">根据运行情况调整</td>
</tr>
<tr>
<td align="left">日志相关</td>
<td align="left">sync_binlog</td>
<td align="left">1、100、0</td>
<td align="left">1安全性最好</td>
</tr>
<tr>
<td align="left">监听及连接</td>
<td align="left">max_connections</td>
<td align="left">根据业务情况调整</td>
<td align="left">可以预留一部分值</td>
</tr>
<tr>
<td align="left">文件读写性能</td>
<td align="left">innodb_flush_log_at_trx_commit</td>
<td align="left">2</td>
<td align="left">安全和性能的折中考虑</td>
</tr>
<tr>
<td align="left">其他</td>
<td align="left">wait_timeout，interactive_timeout</td>
<td align="left">28800</td>
<td align="left">避免应用连接定时中断</td>
</tr>
</tbody></table>
<h3 id="POSTGRES"><a href="#POSTGRES" class="headerlink" title="POSTGRES"></a>POSTGRES</h3><table>
<thead>
<tr>
<th align="left">参数分类</th>
<th align="left">参数名</th>
<th align="left">参数值</th>
<th align="left">备注</th>
</tr>
</thead>
<tbody><tr>
<td align="left">数据缓存</td>
<td align="left">SHARED_BUFFERS</td>
<td align="left">物理内存10-25%</td>
<td align="left"></td>
</tr>
<tr>
<td align="left">数据缓存</td>
<td align="left">CACHE_BUFFER_SIZE</td>
<td align="left">物理内存50-60%</td>
<td align="left"></td>
</tr>
<tr>
<td align="left">日志相关</td>
<td align="left">wal_buffer</td>
<td align="left">8-64M</td>
<td align="left">不建议设置过大过小</td>
</tr>
<tr>
<td align="left">监听及连接</td>
<td align="left">max_connections</td>
<td align="left">根据业务情况调整</td>
<td align="left">一般为业务预估连接数的120%</td>
</tr>
<tr>
<td align="left">其他</td>
<td align="left">maintenance_work_mem</td>
<td align="left">512M或更大</td>
<td align="left"></td>
</tr>
<tr>
<td align="left">其他</td>
<td align="left">work_mem</td>
<td align="left">8-16M</td>
<td align="left">原始配置1M过小</td>
</tr>
<tr>
<td align="left">其他</td>
<td align="left">checkpoint_segments</td>
<td align="left">32或者更大</td>
<td align="left"></td>
</tr>
</tbody></table>
<h3 id="达梦数据库"><a href="#达梦数据库" class="headerlink" title="达梦数据库"></a>达梦数据库</h3><table>
<thead>
<tr>
<th align="left">参数分类</th>
<th align="left">参数名</th>
<th align="left">参数值</th>
<th align="left">备注</th>
</tr>
</thead>
<tbody><tr>
<td align="left">数据缓存</td>
<td align="left">MEMROY_TARGET、MEMROY_POOL</td>
<td align="left">物理内存90%</td>
<td align="left"></td>
</tr>
<tr>
<td align="left">数据缓存</td>
<td align="left">BUFFER</td>
<td align="left">物理内存60%</td>
<td align="left">数据缓存</td>
</tr>
<tr>
<td align="left">数据缓存</td>
<td align="left">MAX_BUFFER</td>
<td align="left">物理内存70%</td>
<td align="left">最大数据缓存</td>
</tr>
<tr>
<td align="left">监听及连接</td>
<td align="left">max_sessions</td>
<td align="left">根据业务需求设置</td>
<td align="left">一般为业务预估连接数的120%</td>
</tr>
</tbody></table>
<h2 id="SQL优化"><a href="#SQL优化" class="headerlink" title="SQL优化"></a>SQL优化</h2><p>SQL优化很容易理解，就是通过给查询字段添加索引或者改写SQL提高其执行效率，一般而言，SQL编写有以下几个通用的技巧：</p>
<ul>
<li>合理使用索引</li>
</ul>
<p><strong>索引少了查询慢；索引多了占用空间大，执行增删改语句的时候需要动态维护索引，影响性能 选择率高（重复值少）且被where频繁引用需要建立B树索引；一般join列需要建立索引；复杂文档类型查询采用全文索引效率更好；索引的建立要在查询和DML性能之间取得平衡；复合索引创建时要注意基于非前导列查询的情况</strong></p>
<ul>
<li>使用UNION ALL替代UNION</li>
</ul>
<p><strong>UNION ALL的执行效率比UNION高，UNION执行时需要排重；UNION需要对数据进行排序</strong></p>
<ul>
<li>避免select * 写法</li>
</ul>
<p><strong>执行SQL时优化器需要将 * 转成具体的列；每次查询都要回表，不能走覆盖索引。</strong></p>
<ul>
<li>JOIN字段建议建立索引</li>
</ul>
<p><strong>一般JOIN字段都提前加上索引</strong></p>
<ul>
<li>避免复杂SQL语句</li>
</ul>
<p><strong>提升可阅读性；避免慢查询的概率；可以转换成多个短查询，用业务端处理</strong></p>
<ul>
<li>避免where 1=1写法</li>
<li>避免order by rand()类似写法</li>
</ul>
<p><strong>RAND()导致数据列被多次扫描</strong></p>
<h3 id="执行计划"><a href="#执行计划" class="headerlink" title="执行计划"></a>执行计划</h3><p>要想优化SQL必须要会看执行计划，执行计划会告诉你哪些地方效率低，哪里可以需要优化。我们以MYSQL为例，来认识一下执行计划。</p>
<p>通过<code>explain sql</code> 可以查看执行计划，如：</p>
<p><img src="https://mmbiz.qpic.cn/mmbiz_png/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZmqM1EWticOELy4ia0mpXbDnJJmkiaKtDvHVgwuXCZ3oyicZdo0cVd7onxg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" alt="图片"></p>
<table>
<thead>
<tr>
<th align="left">字段</th>
<th align="left">解释</th>
</tr>
</thead>
<tbody><tr>
<td align="left">id</td>
<td align="left">每个被独立执行的操作标识，标识对象被操作的顺序，id值越大，先被执行，如果相同，执行顺序从上到下</td>
</tr>
<tr>
<td align="left">select_type</td>
<td align="left">查询中每个select 字句的类型</td>
</tr>
<tr>
<td align="left">table</td>
<td align="left">被操作的对象名称，通常是表名，但有其他格式</td>
</tr>
<tr>
<td align="left">partitions</td>
<td align="left">匹配的分区信息(对于非分区表值为NULL)</td>
</tr>
<tr>
<td align="left">type</td>
<td align="left">连接操作的类型</td>
</tr>
<tr>
<td align="left">possible_keys</td>
<td align="left">可能用到的索引</td>
</tr>
<tr>
<td align="left">key</td>
<td align="left">优化器实际使用的索引(<strong>最重要的列</strong>) 从最好到最差的连接类型为<code>const</code>、<code>eq_reg</code>、<code>ref</code>、<code>range</code>、<code>index</code>和<code>ALL</code>。当出现<code>ALL</code>时表示当前SQL出现了“坏味道”</td>
</tr>
<tr>
<td align="left">key_len</td>
<td align="left">被优化器选定的索引键长度，单位是字节</td>
</tr>
<tr>
<td align="left">ref</td>
<td align="left">表示本行被操作对象的参照对象，无参照对象为NULL</td>
</tr>
<tr>
<td align="left">rows</td>
<td align="left">查询执行所扫描的元组个数（对于innodb，此值为估计值）</td>
</tr>
<tr>
<td align="left">filtered</td>
<td align="left">条件表上数据被过滤的元组个数百分比</td>
</tr>
<tr>
<td align="left">extra</td>
<td align="left">执行计划的重要补充信息，当此列出现<code>Using filesort</code> , <code>Using temporary</code> 字样时就要小心了，很可能SQL语句需要优化</td>
</tr>
</tbody></table>
<h3 id="SQL优化实战"><a href="#SQL优化实战" class="headerlink" title="SQL优化实战"></a>SQL优化实战</h3><p>这里为大家准备了一套SQL优化的综合实战，一步一步带你走一遍完整SQL优化的过程。</p>
<p>在执行优化之前我们需要先认识一下原始表及待优化的SQL。</p>
<ol>
<li>原数据库表结构</li>
</ol>
<figure class="highlight n1ql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> TABLE <span class="symbol">`a`</span></span><br><span class="line">(</span><br><span class="line">    <span class="symbol">`id`</span>          int(<span class="number">11</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> AUTO_INCREMENT,</span><br><span class="line">    <span class="symbol">`seller_id`</span>   bigint(<span class="number">20</span>)                                       DEFAULT <span class="literal">NULL</span>,</span><br><span class="line">    <span class="symbol">`seller_name`</span> varchar(<span class="number">100</span>) CHARACTER <span class="keyword">SET</span> utf8 <span class="keyword">COLLATE</span> utf8_bin DEFAULT <span class="literal">NULL</span>,</span><br><span class="line">    <span class="symbol">`gmt_create`</span>  varchar(<span class="number">30</span>)                                      DEFAULT <span class="literal">NULL</span>,</span><br><span class="line">    <span class="keyword">PRIMARY</span> <span class="keyword">KEY</span> (<span class="symbol">`id`</span>)</span><br><span class="line">);</span><br><span class="line"><span class="keyword">CREATE</span> TABLE <span class="symbol">`b`</span></span><br><span class="line">(</span><br><span class="line">    <span class="symbol">`id`</span>          int(<span class="number">11</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> AUTO_INCREMENT,</span><br><span class="line">    <span class="symbol">`seller_name`</span> varchar(<span class="number">100</span>) DEFAULT <span class="literal">NULL</span>,</span><br><span class="line">    <span class="symbol">`user_id`</span>     varchar(<span class="number">50</span>)  DEFAULT <span class="literal">NULL</span>,</span><br><span class="line">    <span class="symbol">`user_name`</span>   varchar(<span class="number">100</span>) DEFAULT <span class="literal">NULL</span>,</span><br><span class="line">    <span class="symbol">`sales`</span>       bigint(<span class="number">20</span>)   DEFAULT <span class="literal">NULL</span>,</span><br><span class="line">    <span class="symbol">`gmt_create`</span>  varchar(<span class="number">30</span>)  DEFAULT <span class="literal">NULL</span>,</span><br><span class="line">    <span class="keyword">PRIMARY</span> <span class="keyword">KEY</span> (<span class="symbol">`id`</span>)</span><br><span class="line">);</span><br><span class="line"><span class="keyword">CREATE</span> TABLE <span class="symbol">`c`</span></span><br><span class="line">(</span><br><span class="line">    <span class="symbol">`id`</span>         int(<span class="number">11</span>) <span class="keyword">NOT</span> <span class="literal">NULL</span> AUTO_INCREMENT,</span><br><span class="line">    <span class="symbol">`user_id`</span>    varchar(<span class="number">50</span>)  DEFAULT <span class="literal">NULL</span>,</span><br><span class="line">    <span class="symbol">`order_id`</span>   varchar(<span class="number">100</span>) DEFAULT <span class="literal">NULL</span>,</span><br><span class="line">    <span class="symbol">`state`</span>      bigint(<span class="number">20</span>)   DEFAULT <span class="literal">NULL</span>,</span><br><span class="line">    <span class="symbol">`gmt_create`</span> varchar(<span class="number">30</span>)  DEFAULT <span class="literal">NULL</span>,</span><br><span class="line">    <span class="keyword">PRIMARY</span> <span class="keyword">KEY</span> (<span class="symbol">`id`</span>)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>



<ol>
<li>待优化的SQL（查询当前用户在当前时间前后10个小时的订单情况，并根据订单创建时间升序排列）</li>
</ol>
<figure class="highlight mipsasm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">select a.seller_id,</span><br><span class="line">       a.seller_name,</span><br><span class="line">       <span class="keyword">b.user_name,</span></span><br><span class="line"><span class="keyword"></span>       c.state</span><br><span class="line">from a,</span><br><span class="line">     <span class="keyword">b,</span></span><br><span class="line"><span class="keyword"></span>     c</span><br><span class="line">where a.seller_name = <span class="keyword">b.seller_name</span></span><br><span class="line"><span class="keyword"></span>  <span class="keyword">and </span><span class="keyword">b.user_id </span>= c.user_id</span><br><span class="line">  <span class="keyword">and </span>c.user_id = <span class="number">17</span></span><br><span class="line">  <span class="keyword">and </span>a.gmt_create</span><br><span class="line">    <span class="keyword">BETWEEN </span>DATE_ADD(NOW(), INTERVAL – <span class="number">600</span> MINUTE)</span><br><span class="line">    <span class="keyword">AND </span>DATE_ADD(NOW(), INTERVAL <span class="number">600</span> MINUTE)</span><br><span class="line"><span class="keyword">order </span><span class="keyword">by </span>a.gmt_create；</span><br></pre></td></tr></table></figure>



<ol>
<li>原表数据量：</li>
</ol>
<p><img src="https://mmbiz.qpic.cn/mmbiz_png/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZoaX5lLUV2Gibp1ATI99ia0Uyc92Gh29slwOR9NqlXd3AjFQfibC3B5kWA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" alt="图片"></p>
<ol>
<li>原执行时间</li>
</ol>
<p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZMkGXYv94jHf69fRH5LDnBw5cCuDXOrKjEtrCSJK5OLKVsV76VGRwRw/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" alt="图片">0.21s，执行速度还挺快</p>
<ol>
<li>原执行计划</li>
</ol>
<p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZs3xtFkKwUog80SviaOez5vBWrQwvXgFMibjZpAKiaRCFS7sxjV4jObVhA/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" alt="图片">真是糟糕的执行计划。(全表扫描，没有索引；临时表；排序)</p>
<p><strong>初步优化思路：</strong></p>
<ol>
<li>SQL中 where条件字段类型要跟表结构一致，表中<code>user_id</code> 为varchar(50)类型，实际SQL用的int类型，存在隐式转换，也未添加索引。将b和c表<code>user_id</code> 字段改成int类型。</li>
<li>因存在b表和c表关联，将b和c表<code>user_id</code>创建索引</li>
<li>因存在a表和b表关联，将a和b表<code>seller_name</code>字段创建索引</li>
<li>利用复合索引消除临时表和排序</li>
</ol>
<p><strong>初步优化SQL</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> b modify `user_id` <span class="type">int</span>(<span class="number">10</span>) <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>;</span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> c modify `user_id` <span class="type">int</span>(<span class="number">10</span>) <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>;</span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> c <span class="keyword">add</span> index `idx_user_id`(`user_id`);</span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> b <span class="keyword">add</span> index `idx_user_id_sell_name`(`user_id`,`seller_name`);</span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> a <span class="keyword">add</span> index `idx_sellname_gmt_sellid`(`gmt_create`,`seller_name`,`seller_id`);</span><br></pre></td></tr></table></figure>



<p><strong>查看优化后的执行时间</strong></p>
<p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZB3WFry1l3C8ZrM1dBRibGekQyuhicPF73PuZufAdFK8icZxvxZcYWSGZw/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" alt="图片">通过执行计划可以看到，执行时间从0.21s优化成了0.01s，执行时间近乎缩短20倍。</p>
<p><strong>查看优化后的执行计划</strong></p>
<p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZ5WS5TVeo9hicYspiaFhfh8VuXm8KhFqEBwUThibHgG4CMTM8k3x1lIiaJQ/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" alt="图片">执行计划显示从全表扫描优化成了走索引，rows减少，但是此时出现了2个告警。</p>
<p><strong>通过<code>show warning</code>语句 查看告警信息</strong></p>
<p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZbicErgVnxzg5zib5EDfAzIOrresMicBupJqfMrkNAHbQXLH5Nha3dp2yw/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" alt="图片"></p>
<p>提示<code>gmt_crteate</code> 的格式不对，mysql进行了隐式转换导致不能使用索引。</p>
<p><strong>继续优化，修改<code>gmtc-create</code>的格式</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> a modify &quot;gmt_create&quot; datetime <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>;</span><br></pre></td></tr></table></figure>



<p><strong>再次查看执行时间</strong></p>
<p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZbviaibib2lRC2wLsc7V1bhx6jBoKoUL2P2T0utdMiaLPAibxdjpjua2GHqQ/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" alt="图片"></p>
<p><strong>再次查看执行计划</strong></p>
<p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/PxMzT0Oibf4iaicicsWhJbntSpBuNORfVogZq4MvsGjiahxaQWgUep0QZ01iccsTCqZeQAxS9xpwN8dSeX0yKSEAuQkg/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" alt="图片"></p>
<p>至此，我们的优化过程结束，结果非常完美。</p>
<h3 id="SQL优化小结"><a href="#SQL优化小结" class="headerlink" title="SQL优化小结"></a>SQL优化小结</h3><p>这里给大家总结一下SQL优化的套路：</p>
<ol>
<li>查看执行计划 <code>explain sql</code></li>
<li>如果有告警信息，查看告警信息 <code>show warnings;</code></li>
<li>查看SQL涉及的表结构和索引信息</li>
<li>根据执行计划，思考可能的优化点</li>
<li>按照可能的优化点执行表结构变更、增加索引、SQL改写等操作</li>
<li>查看优化后的执行时间和执行计划</li>
<li>如果优化效果不明显，重复第四步操作</li>
</ol>

    </div>
<div>
  
    <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

  
</div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://wan-lei8.gitee.io/2022/01/24/%E6%95%B0%E6%8D%AE%E5%BA%93%E4%BC%98%E5%8C%96%EF%BC%8C%E5%8F%AF%E4%BB%A5%E4%BB%8E%E5%93%AA%E4%BA%9B%E7%BB%B4%E5%BA%A6%E5%85%A5%E6%89%8B/" title="数据库优化，可以从哪些维度入手">https://wan-lei8.gitee.io/2022/01/24/数据库优化，可以从哪些维度入手/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/01/24/%E7%B4%A2%E5%BC%95%E4%BC%98%E5%8C%96/" rel="prev" title="索引优化">
      <i class="fa fa-chevron-left"></i> 索引优化
    </a></div>
      <div class="post-nav-item">
    <a href="/2022/01/25/JVM%E8%B0%83%E4%BC%98/" rel="next" title="JVM调优">
      JVM调优 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9E%B6%E6%9E%84%E4%BC%98%E5%8C%96"><span class="nav-number">1.</span> <span class="nav-text">架构优化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%86%E5%B8%83%E5%BC%8F%E7%BC%93%E5%AD%98"><span class="nav-number">1.1.</span> <span class="nav-text">分布式缓存</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB"><span class="nav-number">1.2.</span> <span class="nav-text">读写分离</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B0%B4%E5%B9%B3%E5%88%87%E5%88%86"><span class="nav-number">1.3.</span> <span class="nav-text">水平切分</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9E%B6%E6%9E%84%E4%BC%98%E5%8C%96%E5%B0%8F%E7%BB%93"><span class="nav-number">1.4.</span> <span class="nav-text">架构优化小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%A1%AC%E4%BB%B6%E4%BC%98%E5%8C%96"><span class="nav-number">2.</span> <span class="nav-text">硬件优化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#DB%E4%BC%98%E5%8C%96"><span class="nav-number">3.</span> <span class="nav-text">DB优化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Oracle"><span class="nav-number">3.1.</span> <span class="nav-text">Oracle</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#MySQL"><span class="nav-number">3.2.</span> <span class="nav-text">MySQL</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#POSTGRES"><span class="nav-number">3.3.</span> <span class="nav-text">POSTGRES</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BE%BE%E6%A2%A6%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="nav-number">3.4.</span> <span class="nav-text">达梦数据库</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#SQL%E4%BC%98%E5%8C%96"><span class="nav-number">4.</span> <span class="nav-text">SQL优化</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%89%A7%E8%A1%8C%E8%AE%A1%E5%88%92"><span class="nav-number">4.1.</span> <span class="nav-text">执行计划</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#SQL%E4%BC%98%E5%8C%96%E5%AE%9E%E6%88%98"><span class="nav-number">4.2.</span> <span class="nav-text">SQL优化实战</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#SQL%E4%BC%98%E5%8C%96%E5%B0%8F%E7%BB%93"><span class="nav-number">4.3.</span> <span class="nav-text">SQL优化小结</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt=""
      src="/images/avatar.png">
  <p class="site-author-name" itemprop="name"></p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">41</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/yourname" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;yourname" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/malegod958@163.com" title="E-Mail → malegod958@163.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2018 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">万磊</span>
</div>
  <div class="powered-by">

  </div>

        








      </div>
    </footer>
  </div>

  
  
  <script color='' opacity='0.5' zIndex='-1' count='99' src="/lib/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

  
	<script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
	

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/clicklove.js"></script>
